<template>
  <div class="app-container">
   

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['setting:process:add']"
        >新增</el-button>
      </el-col>
    </el-row>

      <el-table v-loading="loading" :data="processList" :sortable="true" @selection-change="handleSelectionChange">
      <el-table-column label="原片类型" align="center" prop="originalType" />
      <el-table-column label="原片厚度" align="center" prop="thickness" />
      <el-table-column label="原片颜色" align="center" prop="color" />
      <el-table-column label="磨边量" align="center" prop="grind" />
      <el-table-column label="掰边距" align="center" prop="minWaste" />
      <el-table-column label="最大刀距-X" align="center" prop="max1Cut" />
      <el-table-column label="最小刀距-X" align="center" prop="min1Cut" />
      <el-table-column label="最小刀距-Y" align="center" prop="min2Cut" />
      <el-table-column label="最小刀距-XZ" align="center" prop="min13Cut" />
      <el-table-column label="原片修边量-左" align="center" prop="trimLeft" />
      <el-table-column label="原片修边量-右" align="center" prop="trimRight" />
      <el-table-column label="原片修边量-上" align="center" prop="trimTop" />
      <el-table-column label="原片修边量-下" align="center" prop="trimBottom" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['setting:process:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['setting:process:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工艺参数对话框 -->
    <el-dialog :title="title" v-model="open" width="400px" append-to-body>
      <el-form ref="processRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="原片类型" prop="originalType">
          <el-input v-model="form.originalType" placeholder="请输入原片类型" style="width: 200px" />
        </el-form-item>
        <el-form-item label="原片厚度" prop="thickness">
          <el-input v-model="form.thickness" placeholder="请输入原片厚度" style="width: 200px" />
        </el-form-item>
        <el-form-item label="原片颜色" prop="color">
          <el-input v-model="form.color" placeholder="请输入原片颜色" style="width: 200px" />
        </el-form-item>
        <el-form-item label="磨边量" prop="grind">
          <el-input v-model="form.grind" placeholder="请输入磨边量" style="width: 200px" />
        </el-form-item>
        <el-form-item label="掰边距" prop="minWaste">
          <el-input v-model="form.minWaste" placeholder="请输入掰边距" style="width: 200px" />
        </el-form-item>
        <el-form-item label="最大刀距-X" prop="max1Cut">
          <el-input v-model="form.max1Cut" placeholder="请输入最大刀距-X" style="width: 200px" />
        </el-form-item>
        <el-form-item label="最小刀距-X" prop="min1Cut">
          <el-input v-model="form.min1Cut" placeholder="请输入最小刀距-X" style="width: 200px" />
        </el-form-item>
        <el-form-item label="最小刀距-Y" prop="min2Cut">
          <el-input v-model="form.min2Cut" placeholder="请输入最小刀距-Y" style="width: 200px" />
        </el-form-item>
        <el-form-item label="最小刀距-XZ" prop="min13Cut">
          <el-input v-model="form.min13Cut" placeholder="请输入最小刀距-XZ" style="width: 200px" />
        </el-form-item>
        <el-form-item label="原片修边量-左" prop="trimLeft">
          <el-input v-model="form.trimLeft" placeholder="请输入原片修边量-左" style="width: 200px" />
        </el-form-item>
        <el-form-item label="原片修边量-右" prop="trimRight">
          <el-input v-model="form.trimRight" placeholder="请输入原片修边量-右" style="width: 200px" />
        </el-form-item>
        <el-form-item label="原片修边量-上" prop="trimTop">
          <el-input v-model="form.trimTop" placeholder="请输入原片修边量-上" style="width: 200px" />
        </el-form-item>
        <el-form-item label="原片修边量-下" prop="trimBottom">
          <el-input v-model="form.trimBottom" placeholder="请输入原片修边量-下" style="width: 200px" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Process">
import { listProcess, getProcess, delProcess, addProcess, updateProcess } from "@/api/setting/process";

const { proxy } = getCurrentInstance();

const processList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    originalType: null,
    thickness: null,
    color: null,
    minWaste: null,
    max1Cut: null,
    min1Cut: null,
    min2Cut: null,
    min13Cut: null,
    trimLeft: null,
    trimRight: null,
    trimTop: null,
    trimBottom: null
  },
  rules: {
    createTime: [
      { required: true, message: "原片类型不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询工艺参数列表 */
function getList() {
  loading.value = true;
  listProcess(queryParams.value).then(response => {
    processList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    createrId: null,
    originalType: null,
    thickness: null,
    color: null,
    minWaste: null,
    max1Cut: null,
    min1Cut: null,
    min2Cut: null,
    min13Cut: null,
    trimLeft: null,
    trimRight: null,
    trimTop: null,
    trimBottom: null
  };
  proxy.resetForm("processRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加工艺参数";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getProcess(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改工艺参数";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["processRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateProcess(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProcess(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除工艺参数编号为"' + _ids + '"的数据项？').then(function() {
    return delProcess(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('setting/process/export', {
    ...queryParams.value
  }, `process_${new Date().getTime()}.xlsx`)
}

getList();
</script>
